//载入express

const express = require("express");

//创建路由对象
const  router = express.Router();

const path = require("path");

const fs = require("fs");

router.get("/login",(req,res)=>{
   res.sendFile(path.join(__dirname,"login.html"))
})

router.post("/tologin",(req,res)=>{
    let {tel,pass} = req.body;
    if(tel==""||pass=="")
    {
        res.send("<script>alert('用户名或者密码不得为空')location.href='/login';</script>")
    }
    else
    {
        let content = fs.readFileSync(path.join(__dirname,"../person.json"));
        let userinfo  =  JSON.parse(content.toString()); 
        let result = userinfo.find((value)=>{
            return value.username == tel && value.password == pass;
        })
        if(result)
        {
            res.send("<script>alert('登录成功');</script>");
        }
        else{
            res.send("<script>alert('登录失败');location.href='/login'</script>");
        }
    }
})
module.exports = router;